﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web.Security;

namespace JM.JCY.SharePointComponent.UserManagement.UserPasswordReset
{
    public partial class UserPasswordResetUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    this.ddlUserList.DataSource = proxy.GetUser(0, string.Empty);
                    ddlUserList.DataTextField = "UserName";
                    ddlUserList.DataValueField = "UserName";
                    ddlUserList.DataBind();

                    ListItem lt = new ListItem();
                    lt.Text = "请选择一个用户..";
                    lt.Value = "0";
                    ddlUserList.Items.Insert(0, lt);
                }
            }
        }

        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (ddlUserList.SelectedValue != "0")
            {
                string currentAccount = this.ddlUserList.SelectedValue;
                MembershipUser membershipUser = MembershipProvider.GetUser(currentAccount, false);
                membershipUser.UnlockUser();
                string p = membershipUser.ResetPassword();
                
                if (membershipUser.ChangePassword(p, "123456"))
                {
                    this.lbResetPasswordMessage.Text = "密码重设成功,用户: ‘" + currentAccount + "’ 的密码已经成功重设为: 123456";
                }
                else
                {
                 
                    this.lbResetPasswordMessage.Text = "密码重设失败！";
                }
            }
            else
            {
           
                this.lbResetPasswordMessage.Text = "请选择一个用户，然后点击确认按钮！";
            }
        }


        private static SPIisSettings _SPIisSettings;
        internal static SPIisSettings SPIisSettings
        {
            get
            {
                if (_SPIisSettings == null)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        _SPIisSettings = SPContext.Current.Site.WebApplication.GetIisSettingsWithFallback(SPContext.Current.Site.Zone);
                    });
                }

                return _SPIisSettings;
            }
        }

        internal static MembershipProvider MembershipProvider
        {
            get
            {
                return Membership.Providers[SPIisSettings.FormsClaimsAuthenticationProvider.MembershipProvider];
            }
        }

        internal static RoleProvider RoleProvider
        {
            get
            {
                return Roles.Providers[SPIisSettings.FormsClaimsAuthenticationProvider.RoleProvider];
            }
        }
    }
}
